package com.gxuwu.admin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxuwu.admin.entity.MyUser;
import com.gxuwu.admin.service.MyUserService;
import com.gxuwu.admin.utils.JwtUtils;
import com.gxuwu.admin.utils.R;
import com.gxuwu.admin.utils.ResponseUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@Api(tags = "后台登录与权限管理")
@RestController
@RequestMapping("/admin/acl/index")
public class IndexController {


    @Autowired
    private MyUserService userService;


    @PostMapping("/logout")
    public R logout(){


        return R.ok();
    }


    @GetMapping("/info")
    public R getUserInfo(HttpServletResponse response){
        String token = response.getHeader("token");

        String username = new JwtUtils().getUsernameFromToken(token);



        return R.ok().data("userInfo",username);
    }

    /**
     * 登录方法
     * @param user 用户名

     * @return
     */
    @PostMapping("/login")
    public R login(HttpServletResponse response,@RequestBody MyUser user){

        System.out.println(user.getUsername());
        String token=userService.login(user.getUsername(),user.getPassword());


        return R.ok().data("token",token);
    }
}
